﻿package andy.demo.notification;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import andy.demo.R;

public class StartNotificationActivity extends Activity {

	private static final int HELLO_ID = 1;
	private Notification notification;
	protected NotificationManager mNotificationManager;
	protected PendingIntent contentIntent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.start_notification);
		Button start = (Button)this.findViewById(R.id.start);
		Button update = (Button)this.findViewById(R.id.update);
		start.setOnClickListener(new OnClickListener() {
			
		

			@Override
			public void onClick(View v) {
//				1.Get a reference to the NotificationManager: 
				String ns = Context.NOTIFICATION_SERVICE;
				mNotificationManager = (NotificationManager) getSystemService(ns);
				
//				2.Instantiate the Notification: 
				int icon = R.drawable.icon;
				CharSequence tickerText = "Hello";
				long when = System.currentTimeMillis();
				notification = new Notification(icon, tickerText, when);
				
//				3.Define the Notification's expanded message and Intent: 
				Context context = getApplicationContext();
				CharSequence contentTitle = "My notification";
				CharSequence contentText = "Hello World!";
				Intent notificationIntent = new Intent(StartNotificationActivity.this, ReceiveNotificationActivity.class);
				contentIntent = PendingIntent.getActivity(StartNotificationActivity.this, 0, notificationIntent, 0);
				notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
				
//				4.Pass the Notification to the NotificationManager: 
				mNotificationManager.notify(HELLO_ID, notification);
				
			}
		});
		
		update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				notification.defaults |= Notification.DEFAULT_SOUND;
				
				long[] vibrate = {0,100,200,300};
				notification.vibrate = vibrate;
				
				notification.ledARGB = 0xff00ff00;
				notification.ledOnMS = 300;
				notification.ledOffMS = 1000;
				notification.flags |= Notification.FLAG_SHOW_LIGHTS;
				
				notification.setLatestEventInfo(getApplicationContext(), "B", "BB", contentIntent);
				mNotificationManager.notify(HELLO_ID, notification);
			}
		});
		
	}

	
}
